package com.jsu.controller;

import com.jsu.sender.MessageSender;
import com.jsu.service.ActivityService;
import com.jsu.service.StreamingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.FrameRecorder;
import org.example.model.activity.dto.ActivityWithFlowDTO;
import org.example.model.activity.pojo.ActivityArticle;
import org.example.model.activity.pojo.Registration;
import org.example.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/api/activity")
@Api(tags = "活动模块控制器")
public class ActivityController {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private StreamingService streamingService;

    @ApiOperation(value = "活动查询")
    @GetMapping("/getAllActivities")
    public ResponseResult getAllActivities(Integer type,Integer userId){
        return activityService.getAllActivities(type,userId);
    }

    @ApiOperation(value = "活动名字搜索")
    @GetMapping("/getActivitysByName/{activityName}")

    public ResponseResult getActivitysByName(@PathVariable("activityName") String activityName){
        return activityService.getActivitysByName(activityName);
    }

    @ApiOperation(value = "根据活动种类名查询")
    @GetMapping("/getActivityByName")
    public ResponseResult getActivityByName(Integer type){
        return activityService.getActivityByName(type);
    }

    @ApiOperation(value = "根据id查询活动具体信息")
    @GetMapping("/getActivityById")
    public ResponseResult getActivityById(Integer activityId){
        return activityService.getActivityById(activityId);
    }

    @ApiOperation(value = "根据id查询活动报名信息")
    @GetMapping("/getRegistrationByActivityId")
    public ResponseResult getRegistrationByActivityId(Integer activityId){
        return activityService.getRegistrationByActivityId(activityId);
    }

    @ApiOperation(value = "拿到文章信息")
    @GetMapping("/getArticle")
    public ResponseResult getArticle(Integer activityId){
        return activityService.getArticleToHtml(activityId);
    }

    @ApiOperation(value = "活动文章发布")
    @PostMapping("/addArticle")
    public ResponseResult addArticle(@RequestBody ActivityArticle activityArticle) {
        return activityService.addArticle(activityArticle);
    }
    @ApiOperation(value = "活动新增")
    @PostMapping("/addActivity")
    public ResponseResult addActivity(@RequestBody ActivityWithFlowDTO activityDTO){
        return activityService.addActivity(activityDTO);
    }
    @ApiOperation(value = "活动编辑")
    @PutMapping("/updateActivityPic")
    public ResponseResult updateActivity(@RequestBody ActivityWithFlowDTO activityWithFlowDTO){
        return activityService.updateActivity(activityWithFlowDTO);
    }

    @ApiOperation(value = "活动删除")
    @DeleteMapping("/delActivity")
    public ResponseResult delActivity(Integer activityId){
        return activityService.delActivity(activityId);
    }

    @ApiOperation(value = "活动点赞状态更新")
    @PutMapping("/addActivityLikes")
    public ResponseResult updateActivityLikes(Integer activityId, Integer userId, Integer isLiked){
        return activityService.updateActivityLikes(activityId,userId,isLiked);
    }

    @ApiOperation(value = "活动浏览")
    @PutMapping("/addActivityViews")
    public ResponseResult addActivityViews(Integer activityId){
        return activityService.addActivityViews(activityId);
    }

    @ApiOperation(value = "活动下一篇")
    @GetMapping("/getNextActivity")
    public ResponseResult getNextActivity(Integer activityId){
        return activityService.getNextActivity(activityId);
    }




    @ApiOperation(value = "活动报名")
    @PostMapping("/registration")
    public ResponseResult registration(@RequestBody Registration registration){
        return activityService.registration(registration);
    }

    @ApiOperation(value = "活动报名列表查询")
    @GetMapping("/getRegistrationList")
    public ResponseResult getRegistrationList(){
        return activityService.getRegistrationList();
    }

    @GetMapping("/takePhoto")
    @ApiOperation(value = "拍照上传识别")
    public ResponseResult takePhoto(Integer activityId,Integer userId,String base64Img){
        return activityService.takePhoto(activityId,userId,base64Img);
    }

    @PostMapping("/start-live-stream")
    @ApiOperation(value = "开启会议直播")
    public ResponseResult startLiveStream(@ApiParam(value = "会议id")@RequestParam("activityId") int id,
                                          @ApiParam(value = "会议名")@RequestParam("activityName")String name){

        try {
            streamingService.recordWebcamAndMicrophone(id);
            MessageSender.sendAcitivityLive(name, String.valueOf(id));
        } catch (FrameGrabber.Exception e) {
            throw new RuntimeException("开启失败！");
        } catch (FrameRecorder.Exception e) {
            throw new RuntimeException("开启失败");
        }
        return ResponseResult.okResult("开启成功！");
    }

    @PostMapping("/stop-live-stream")
    @ApiOperation(value = "关闭会议直播")
    public ResponseResult stopLiveStream(){

        try {
            streamingService.stopLive();
        }catch (Exception e){
            throw new RuntimeException("关闭直播失败！");
        }
        return ResponseResult.okResult("关闭直播成功！");
    }
}
